package com.example.common.vm

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.common.intent.BaseViewIntent
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch

abstract class BaseViewModel<I: BaseViewIntent> : ViewModel(){
    //初始化管道
    var channel: Channel<I> = Channel(Channel.UNLIMITED)

    init {
        viewModelScope.launch {
            channel.consumeAsFlow().collect{
                dispatchIntent(it)
            }
        }
    }

    abstract fun dispatchIntent(intent: I)

}